package Chat;
import Chat.A_Chat_Server;
import Chat.CollisionCheck;
import java.io.*;
import java.net.*;
import java.util.Scanner;

import windows.GameConsole;

import controllers.Map;


public class A_Chat_Server_Return implements Runnable
{
	//Globals
	Socket SOCK;
	private Scanner INPUT;
	private PrintWriter OUT;
	String MESSAGE = "";
	public static String TEMP0;
	public static String TEMP1;
	
	public A_Chat_Server_Return(Socket X)
	{
		this.SOCK = X;
	}
	

	public void CheckConnection() throws IOException
	{
		if(!SOCK.isConnected())
		{
			for(int i =1; i <= A_Chat_Server.ConnectionArray.size(); i++)
			{
				if(A_Chat_Server.ConnectionArray.get(i) == SOCK)
				{
					A_Chat_Server.ConnectionArray.remove(i);
				}
			}
			for(int i = 1; i <= A_Chat_Server.ConnectionArray.size(); i++)
			{
				Socket TEMP_SOCK = (Socket) A_Chat_Server.ConnectionArray.get(i-1);
				PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
				TEMP_OUT.println(TEMP_SOCK.getLocalAddress().getHostName() + " disconnected!");
				TEMP_OUT.flush();
				//Show disconnected at SERVER
				System.out.println(TEMP_SOCK.getLocalAddress().getHostName() + " disconnected!");
			}
		}
	}
	public void run()
	{
		try
		{
			try{
				INPUT = new Scanner(SOCK.getInputStream());
				OUT = new PrintWriter(SOCK.getOutputStream());
				
				while(true)
				{
					CheckConnection();
					if(!INPUT.hasNext())
					{
						return;
					}
					
					MESSAGE = INPUT.nextLine();
					
					/*if(MESSAGE.contains("$@")){
						if(TEMP0 == null)
						{
							TEMP0 = MESSAGE;	
						}
						else
						{
							TEMP1 = MESSAGE;
							CollisionCheck.Collision2(TEMP0, TEMP1); 
						}
					}
					*/
					System.out.println("Client said: " + MESSAGE);
					
					for(int i = 1; i <= A_Chat_Server.ConnectionArray.size(); i++)
					{
						Socket TEMP_SOCK = (Socket) A_Chat_Server.ConnectionArray.get(i-1);
						PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
						TEMP_OUT.println(MESSAGE);
						TEMP_OUT.flush();
						System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName());						
					}
				}
			}
			finally
			{
				SOCK.close();
			}
		}
		catch(Exception X) 
		{
			System.out.println(X);
		}
	}
}
	/*public void run()
	{
		try
		{
			try{
				INPUT = new Scanner(SOCK.getInputStream());
				OUT = new PrintWriter(SOCK.getOutputStream());
				
				
					CheckConnection();
					if(!INPUT.hasNext())
					{
						return;
					}
										
					
					//for (int p = 0; p < 1; p++) 
					//{
						
						MESSAGE = INPUT.nextLine();										
					
					
						//if(MESSAGE.contains("$@"))
							//{	
								//TEMP3[p] = MESSAGE;
								//CollisionCheck.Collision2(TEMP3[0], TEMP3[1]);
								//p=1;
							//}
						//else
							//{
								//p=0;
							//}
					
					
						System.out.println("Client said: " + MESSAGE);
					
						for(int i = 1; i <= A_Chat_Server.ConnectionArray.size(); i++)
						{
							Socket TEMP_SOCK = (Socket) A_Chat_Server.ConnectionArray.get(i-1);
							PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
							TEMP_OUT.println(MESSAGE);
							TEMP_OUT.flush();
							System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName());						
						}
					//}
				}			
			finally
			{
				SOCK.close();
			}
		}
		catch(Exception X) 
		{
			System.out.println(X);
		}
	}
}

*/